<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
  
        <el-form-item label="位置名称" prop="locationName">
          <el-input
            v-model="queryParams.locationName"
            placeholder="请输入位置名称"
            clearable
            size="small"
            style="width: 180px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        
  
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增任务</el-button>
        </el-col>
        
        <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['basedata:bclass:remove']"
          >删除</el-button>
        </el-col> -->
  
      </el-row>
  
      <el-table v-loading="loading" :data="bclassList">
        <el-table-column label="任务名称" align="center" width="220" prop="jobName" :show-overflow-tooltip="true" />
        <el-table-column label="任务状态" align="center" width="100" prop="status" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
                <el-tag v-if="scope.row.status=='NORMAL'" type="success">正常</el-tag>
                <el-tag v-if="scope.row.status=='PAUSED'" type="info">暂停</el-tag>
                <el-tag v-if="scope.row.status=='ERROR'" type="danger">任务出错</el-tag>
                
            </template>
        </el-table-column>
        <el-table-column label="任务所在组" align="center"  prop="jobGroupName" :show-overflow-tooltip="true" />
        <el-table-column label="任务类名" align="center" prop="jobClassName" :show-overflow-tooltip="true" />
        <el-table-column label="触发器名称" align="center"  prop="triggerName" :show-overflow-tooltip="true" />
        <el-table-column label="Cron表达式" align="center" prop="cronExpression" :show-overflow-tooltip="true" />
        <el-table-column label="时区" align="center"  prop="timeZone" :show-overflow-tooltip="true" />

        <el-table-column label="操作" align="center" fixed="right" width="230">
          <template slot-scope="scope">
            
            <el-tooltip class="item" effect="dark" content="立即执行" placement="top">
              <el-button
                    size="mini"
                    circle type="warning"
                    icon="el-icon-caret-right"
                    @click="execute(scope.row)"
                   
              />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="暂停" placement="top">
              <el-button
                    size="mini"
                    circle type="info"
                    icon="el-icon-video-pause"
                    @click="handlePause(scope.row)"
                   
              />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="开启" placement="top">
              <el-button
                    size="mini"
                    circle type="success"
                    icon="el-icon-video-play"
                    @click="handleResume(scope.row)"
                    
              />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button
                    size="mini"
                    circle type="primary"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                   
              />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button
                    size="mini"
                    circle type="danger"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    
              />
            </el-tooltip>
            
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="60%">
         <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                  <el-form-item label="任务名称" prop="jobName">
                      <el-input v-model="form.jobName" :disabled="isUpdate"></el-input>
                  </el-form-item>
              </el-col>
              <el-col :span="12" >
                  <el-form-item label="任务分组" prop="jobGroupName">
                    <el-select v-model="form.jobGroupName">
                        <el-option  label="myJob" value="myJob"></el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                  <el-form-item label="任务类名" prop="jobClassName">
                      <el-input v-model="form.jobClassName" ></el-input>
                  </el-form-item>
              </el-col>  
              <el-col :span="12">
                  <el-form-item label="Cron表达式" prop="cronExpression">
                      <el-input v-model="form.cronExpression" ></el-input>
                  </el-form-item>
              </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { queryjob, deletejob, resumejob, pausejob,addjob,executeTask} from "@/api/sysconfig/job";
  
  export default {
    name: "BClass",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        stuCount: 0,
        // 字典表格数据
        bclassList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 班主任
        userOptions: [],
        // 状态数据字典
        statusOptions: [],
        // 班级所属部门
        classDeptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          locationPid: 0,
          status: undefined,
          classDept:undefined
        },
        // 表单参数
        form: {
          sex: '1'
        },
        // 表单校验
        rules: {
            jobName: [ { required: true, message: "请填写任务名称", trigger: "blur" } ],
            jobGroupName: [ { required: true, message: "请选择任务组", trigger: "blur" } ],
            jobClassName: [ { required: true, message: "请填写任务类名", trigger: "blur" } ],
            cronExpression: [ { required: true, message: "请Cron表达式", trigger: "blur" } ]
        },
        selectedList:[],
        isUpdate:false
      };
    },
    created() {
      this.getList();
    },
    methods: {
      execute(row){
        executeTask({
          jobName: row.jobName,
          jobGroupName: row.jobGroupName
        }).then(resp=>{
          this.msgSuccess("操作成功")
        })
      },
      /** 查询班级列表 */
      getList() {
        this.loading = true;
        this.bclassList=[]
        queryjob(this.queryParams).then(response => {
            this.bclassList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
            locationPid:0,
            locationName:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        if(this.queryParams.locationName){
            this.queryParams.locationPid=null
        }else{
            this.queryParams.locationPid=0
        }
        
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");

        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.open = true;
        this.title = "添加空间位置";
        this.form.locationPid = Number(row.id)
        this.disabledInput=false;
        this.isUpdate = false
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.open = true
        this.isUpdate = true
        this.form = row
        this.form.locationPid = Number(row.locationPid)

      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addjob(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("提交成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.message);
                }
              });
            
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const param ={
            jobClassName: row.jobName,
            jobGroupName: row.jobGroupName
        }
        this.$confirm('是否确认删除"' + row.jobName + '"任务吗?', "警告", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }).then(function() {
                  return deletejob(param);
                }).then(() => {
                  this.getList();
                  this.msgSuccess("删除成功");
                }).catch(function() {});
  
      },
      handlePause(row){//暂停
        const param ={
            jobClassName: row.jobName,
            jobGroupName: row.jobGroupName
        }
        pausejob(param).then(resp=>{
            this.msgSuccess("暂停成功");
            this.getList();
            
        })
      },
      handleResume(row){//恢复
        const param ={
            jobClassName: row.jobName,
            jobGroupName: row.jobGroupName
        }
        resumejob(param).then(resp=>{
            this.msgSuccess("成功开启");
            this.getList();
            
        })
      }
    }
  };
  </script>
  